import 'package:flutter/material.dart';
import '../../application.dart';
import '../../models/article_model.dart';
import '../../models/category_model.dart';
import '../../widgets/article_widget.dart';

class CateRow extends StatelessWidget {
  final String channel;
  final CategoryModel cate;
  final List<ArticleModel>? lists;

  const CateRow({
    Key? key,
    required this.cate,
    required this.lists,
    required this.channel,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          children: [
            Expanded(
              child: Text(
                cate.title,
                style: Theme.of(context).textTheme.titleMedium,
              ),
            ),
            TextButton(
              onPressed: () {
                app.route(
                  '/channel/list',
                  arguments: {'channel': channel, 'cate': cate.id},
                );
              },
              child: const Text('MORE >'),
            ),
          ],
        ),
        SizedBox(
          height: 130,
          child: GridView.builder(
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 1,
              childAspectRatio: 0.35,
            ),
            itemCount: (lists?.length ?? 0) + 1,
            scrollDirection: Axis.horizontal,
            primary: false,
            itemBuilder: (context, index) {
              if (index >= (lists?.length ?? 0)) {
                return TextButton(
                  onPressed: () {
                    app.route(
                      '/channel/list',
                      arguments: {'channel': channel, 'cate': cate.id},
                    );
                  },
                  child: const Text('查看更多 >'),
                );
              }
              return SizedBox(
                width: MediaQuery.of(context).size.width * 0.7,
                child: ArticleWidget(lists![index]),
              );
            },
          ),
        ),
      ],
    );
  }
}
